//
//  myTabBarVC.swift
//  swift_weibo
//
//  Created by ccl on 16/2/16.
//  Copyright © 2016年 LittleBoy. All rights reserved.
//

import UIKit

class myTabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setValue(CC_Maintabar(), forKeyPath: "tabBar")
        
        //creatLoadSettingVC()
        addChildViewControllers()
    }

 
  /*
   // 动态加载 但是cls 不会指定是UIControllerView 所以 暂时没法往下写
///  加载控制器数据
    private func creatLoadSettingVC() {
    
        let path = NSBundle.mainBundle().pathForResource("CCTabBarVCSeting.json", ofType:nil)!
      let data = NSData(contentsOfFile: path)!
        /// try!  强制执行, 如果不成功, 直接崩溃...
        /// do { try 语句 } catch { 失败处理 }
        
        do {
        let dataAry = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
            
            for dataDic in dataAry as! [[String : String]]{
              print(dataDic)
            addChildViewController(dataDic["vcStr"]!, title: dataDic["title"]!, imageName: dataDic["imageName"]!)
            
            }
            
        }catch{
            // 失败不会崩溃,走这里的语句
           print(error)
            
            
        }
    }
    
    ///  动态加载控制器
    ///
    ///  - parameter vcTitle:   控制器的类名字
    ///  - parameter title:     控制器的title
    ///  - parameter imageName: 控制器的图片
    private func addChildViewController(vcTitle:String, title:String, imageName:String){
        // 先加在项目默认名称
        let bundleExecutable = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        
        let cls : AnyClass = NSClassFromString(bundleExecutable + "." + vcTitle)!
        
        
        let vc = cls.new() as! UIViewController ;
        vc.title = title
        vc.tabBarItem.image = UIImage(named:imageName)
        vc.tabBarItem.selectedImage = UIImage(named:imageName + "highlight")
          addChildViewController()
    }
 */
   
 
     //  静态添加控制器方法
    /**
     添加所有控制器
     */
    private func addChildViewControllers (){
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
          addChildViewController(discoverTableViewVC(), title: "消息", imageName: "tabbar_message_center")
          addChildViewController(messageTableViewVC(), title: "发现", imageName: "tabbar_discover")
          addChildViewController(profileTableViewVC(), title: "我", imageName: "tabbar_profile")
    }
    
    /**
     添加子控制器
     */
    private func addChildViewController(vc:UIViewController , title:String, imageName:String) {
        tabBar.tintColor = UIColor.orangeColor()
            
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "highlight")
        let nav = UINavigationController(rootViewController:vc)
        addChildViewController(nav)
        
    }
  
}
